package com.yikome.utils;

import java.util.Iterator;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

public class NamedParameterJdbcUtils {

	public static String findOrderSQL(Pageable pageable) {
		
		StringBuilder orderSQL = new StringBuilder();
        Iterator<org.springframework.data.domain.Sort.Order> iterator = pageable.getSort().iterator();
        while (iterator.hasNext()) {
            Sort.Order order = (Sort.Order) iterator.next();
            if (orderSQL.length() != 0) {
                orderSQL.append(" , ");
            }
            orderSQL.append(order.getProperty()).append(" ").append(order.getDirection()).append(" ");
        }
        if (orderSQL.length() > 0) {
        	orderSQL.insert(0, " ORDER BY ") ;
        	orderSQL.append(" \n");
        }
		
        return orderSQL.toString();
	}
	
	public static String findPageSQL(Pageable pageable) {
		
		return "LIMIT " + pageable.getOffset() + " , " + pageable.getPageSize() + " \n";
	}
}
